package com.example.javaui.component;

import com.example.javaui.ResourceTable;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.*;
import ohos.agp.utils.Color;
import ohos.agp.utils.Point;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Rect;
import ohos.media.image.common.Size;

import java.io.InputStream;

public class NeumorCircleButton extends Component implements Component.DrawTask {


    // HiLogLabel
    private static final HiLogLabel sLogLabel = new HiLogLabel(HiLog.LOG_APP, 0x00101, "NeumorButton");


    public NeumorCircleButton(Context context) {
        super(context);
        initView();
    }

    public NeumorCircleButton(Context context, AttrSet attrSet) {
        super(context, attrSet);
        initView();
    }

    public NeumorCircleButton(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        initView();
    }

    public NeumorCircleButton(Context context, AttrSet attrSet, int resId) {
        super(context, attrSet, resId);
        initView();
    }

    private void initView() {
        this.addDrawTask(this);
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {

        int width = getWidth();
        int height = getWidth();
        int shadowWidth = 40;
        int centerX = width / 2;
        int centerY = height / 2;
        int realWidth = width - shadowWidth * 2;
        int realHeight = height - shadowWidth * 2;

        canvas.drawCircle(centerX, centerY, realWidth / 2, newCirclePaint(centerX, centerY, shadowWidth));
        canvas.drawCircle(centerX, centerY, realWidth / 2, newCirclePaint(centerX, centerY, shadowWidth));

        /*
        int textSize = 80;
        Paint textPaint = new Paint();
        textPaint.setTextSize(textSize);
        canvas.drawText(textPaint, "√", centerX - textSize / 4, centerY + textSize / 3);

         */

        int pw = 100; int ph = 100;
        PixelMap pixelMap = getPixelMap(ResourceTable.Media_ic_select, pw, ph);
        PixelMapHolder holder = new PixelMapHolder(pixelMap);
        canvas.drawPixelMapHolder(holder, centerX - pw / 2, centerY - ph / 2, new Paint());

    }

    private static Paint newCirclePaint(int centerX, int centerY, int shadowWidth) {

        Paint paint = new Paint();
        Point point1 = new Point(centerX - shadowWidth,centerY -shadowWidth );
        Point point2 = new Point(centerX + shadowWidth,centerY +shadowWidth );
        Point[] newPoints = new Point[]{point1, point2};
        Color[] newColors = new Color[]{Color.WHITE, Color.GRAY};
        Shader.TileMode tileMode = Shader.TileMode.CLAMP_TILEMODE;
        LinearShader shader = new LinearShader(newPoints, null, newColors, tileMode);
        paint.setShader(shader, Paint.ShaderType.LINEAR_SHADER);
        MaskFilter maskFilter = new MaskFilter(shadowWidth - 5, MaskFilter.Blur.OUTER);
        paint.setMaskFilter(maskFilter);
        return paint;

    }


    /**
     * 打印信息
     * @param output
     */
    private static void log(String output) {
        HiLog.info(sLogLabel, output);
    }

    /**
     * 通过资源ID获取位图对象
     **/
    private PixelMap getPixelMap(int drawableId, int desiredWidth, int desiredHeight) {

        InputStream drawableInputStream = null;
        try {
            drawableInputStream = getContext().getResourceManager().getResource(drawableId);
            ImageSource.SourceOptions sourceOptions = new ImageSource.SourceOptions();
            sourceOptions.formatHint = "image/png";
            ImageSource imageSource = ImageSource.create(drawableInputStream, sourceOptions);
            ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
            decodingOptions.desiredSize = new Size(desiredWidth, desiredHeight);
            decodingOptions.desiredRegion = new Rect(0, 0, 0, 0);
            decodingOptions.desiredPixelFormat = PixelFormat.ARGB_8888;
            PixelMap pixelMap = imageSource.createPixelmap(decodingOptions);
            return pixelMap;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try{
                if (drawableInputStream != null){
                    drawableInputStream.close();
                }
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
